<?php

/*
 * litchicms
 * Copyright (C) ${date} Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/**
  获得所有权限组
 * @return array
 */
function brk_get_group() {
    $GROUP = array();
    $rs = brk_select("group", "*", " ORDER BY group_id ASC");
    while ($row = $GLOBALS['Db']->fetch_array($rs)) {
        $GROUP[$row['group_id']] = $row;
    }
    return $GROUP;
}

/**
 * 获取所有权限
 */
function brk_get_rights() {
    $RIGHT = array();
    $rs = brk_select("right");
    while ($row = $GLOBALS['Db']->fetch_array($rs)) {
        $RIGHT[$row['right_id']] = $row;
    }
    return $RIGHT;
}

/**
 * 获取一个组的所有权限
 */
function brk_get_group_rigths($group_id) {
    $group_right = array();
    $rs = brk_select('group_right', '*', " WHERE group_id='" . intval($group_id) . "'");
    while ($row = $GLOBALS['Db']->fetch_array($rs)) {
        $group_right[$row['right_id']] = 0;
    }
    return $group_right;
}

/*
 * 生成某个组的配置文件
 */

function write_conf_file($group_id) {
    //前期实现后台的权限
    $RIGHT = array();
    if ($group_id == 65535) {
        $rs = brk_select("right");
    } else {
        $rs = brk_select_join("right,group_right", "{0}.*", "INNER JOIN {1} ON {0}.right_id={1}.right_id WHERE {1}.group_id ='" . $group_id . "'");
    }
    $RIGHT = array();
    while ($row = $GLOBALS['Db']->fetch_array($rs)) {
        $RIGHT[$row['right_id']] = $row;
    }
    !class_exists('tree') && require 'tree.class.php';

    $tree = new Tree($RIGHT, 'right_id', 'right_pid', 'right_note');
    $str = "<?php\n!defined('BRK') && exit('!');\n";
    !array_key_exists(0, $RIGHT) && $RIGHT[0] = array('right_note' => '_root', 'right_node' => '0', 'right_pid' => '-1');
    $temparr = _format_array_string(0, $tree, $RIGHT);

//    $str.="\$BRK_GLOBAL['RIGTHS']['note']=array(\n" . $temparr[0] . ");\n";
//    $str.="\$BRK_GLOBAL['RIGTHS']['node']=array(\n" . $temparr[1] . ");\n";
//    $str.="\$BRK_GLOBAL['RIGTHS']['child'] = array(\n" . $temparr[2] . ");\n";
//    $str.="\$BRK_GLOBAL['RIGTHS']['parent'] = array(\n" . $temparr[3] . ");\n";

    $temparr['note'][-2] = '当前没有操作';
    $temparr['node'][-2] = 'noaction';

    ksort($temparr['note']);
    ksort($temparr['node']);
    ksort($temparr['child']);
    ksort($temparr['parent']);
    $str.="return " . var2string($temparr) . ";\n";
    file_put_contents(SYSTEM . "data/group_rigths/$group_id.php", $str);
    unset($tree);

    //TODO:前台权限
    unset($RIGHT);
    return true;
}

/*
 * 生成某个组的配置文件__辅助递归
 */

function _format_array_string($id, $tree, $RIGHT) {
    static $o_array = array('note' => array(), 'node' => array(), 'child' => array(), 'parent' => array());
    $o_array['note'][$id] = $RIGHT[$id]['right_note'];
    $o_array['node'][$id] = $RIGHT[$id]['right_node'];
    $o_array['parent'][$id] = $RIGHT[$id]['right_pid'];

    $nodes = $tree->getChild($id);
    sort($nodes);
    if (!empty($nodes)) {
        $o_array['child'][$id] = array();
        foreach ($nodes as $k => $v) {
            $o_array['child'][$id][$k] = $RIGHT[$v]['right_id'];
        }
        foreach ($nodes as $k => $v) {
            _format_array_string($v, $tree, $RIGHT);
        }
    }
    return $o_array;
}

